//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		pagetitle:app.setting.title,
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		account:app.setting.account,
		action:'index',
		page:1,
		page_id:'',
		pagedata: {},
		region: ['广东省', '广州市', '海珠区'],
		onShowAuthSetting: false,
		isnodata: false,
		articles:[],
		fenleis:[],
		products:[],
		post:[],
		productfenleis:[]
	},
	onReady: function () {
      //获得Modal
	},
	onShow: function (){
		var ssid=wx.getStorageSync('ssid');
		if(!ssid && !app.issession){
			app.util.session();
		}
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var the=this;
		var data={};
		var footer={};
		wx.setNavigationBarTitle({'title':the.data.pagetitle});
		var route=the.route;
		the.setData({
			route:route,
			pagedata: data,
			diydata: data,
			'post.fileddata':[],
			footer:footer,
			nodatapic_close:true,
			root:''
		});
		if(opt.isgoshare){
				the.setData({
					ShowGoshare:true,
				});
		}
		if(opt.fromuser){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		the.get_articles();
		the.get_fenleis();
		the.get_products();
		the.get_productfenleis();
	},
	// 进云常获取数据
	get_productfenleis: function(e){
		var that = this;
		var productfenleis = that.data.products
		app.util.get('jy_weishop/goods.index/main/',function(res){
			//加载分页
			if(res.list){
					productfenleis = res.category;
			}
			that.setData({
					productfenleis: productfenleis,
			});
		});
	},
	get_products: function(e){
		var that = this;
		var page = that.data.page
		var products = that.data.products
		var isnodata = that.data.isnodata
		app.util.get('jy_weishop/goods.index/getlist/page='+page,function(res){
			//加载分页
			if(res){
					products = products.concat(res);
					if(page >1){
						$(".list-number").animate({scrollTop:0},300);
					}
					page++;
			}else{
					isnodata = true
			}
			that.setData({
					products: products,
					isnodata: isnodata,
					page: page
			});
		});
	},
	get_fenleis: function(e){
		var that = this;
		var fenleis = that.data.fenleis
		var isnodata = that.data.isnodata
		app.util.get('jy_weishop/goods.index/main/',function(res){
			//加载分页
			if(res.list){
					fenleis = fenleis.concat(res.category);
			}else{
					isnodata = true
			}
			that.setData({
					fenleis: fenleis,
					isnodata: isnodata
			});
		});
	},
	get_articles: function(e){
		var that = this;
		var page = that.data.page
		var articles = that.data.articles
		var isnodata = that.data.isnodata
		app.util.get('core/article.list//page='+page,function(res){
			//加载分页
			if(res.list){
					articles = articles.concat(res.list);
					if(page >1){
						$(".list-number").animate({scrollTop:0},300);
					}
					page++;
			}else{
					isnodata = true
			}
			that.setData({
					articles: articles,
					isnodata: isnodata,
					page: page
			});
		});
	},
	choose_file:function(e){
		var the =this
		var modaldata=the.data.modaldata?the.data.modaldata:{}
		if(modaldata.params){
			var title= modaldata.params.title?modaldata.params.title:'';
		}else{
			var title='';
		}
		modaldata.type=modaldata.type?modaldata.type:'one';
		modaldata.source_type=modaldata.source_type?modaldata.source_type:'image';
		if(e && e.currentTarget && e.currentTarget.dataset){
			var data=e.currentTarget.dataset;
			var op=data.op?data.op:'';
			var page=data.page?data.page:1;
			var group_id=data.group_id?data.group_id:'';
			if(!group_id && modaldata.params && modaldata.params.group_id){
				group_id=modaldata.params.group_id;
			}
			if(data.file){
				modaldata.filename=data.file;
			}
			if(data.tovalue){
				modaldata.tovalue=data.tovalue;
			}
			if(data.thumb){
				modaldata.thumbname=data.thumb;
			}
			modaldata.index=data.index?data.index:0;
			modaldata.type=data.type?data.type:'one';
			modaldata.source_type=data.source_type?data.source_type:modaldata.source_type;
		}else{
			var op=modaldata.op?modaldata.op:'list';
			var group_id=modaldata.params.group_id?modaldata.params.group_id:-2;
			var page=1;
		}
		var action='core/tools.choose_file//type='+modaldata.type+'&op='+op+'&source_type='+modaldata.source_type+'&group_id='+group_id+'&title='+title+'&page='+page
		app.util.get(action,function(res){
			modaldata.file_list=modaldata.file_list?modaldata.file_list:[];
			modaldata=Object.assign(modaldata,res);
			the.setData({
				modaldata: modaldata,
				showchoosefile:true,
			})
		});
	},
	choose_file_tabop:function(e){
		var the =this
		var modaldata =the.data.modaldata
		var data=e.currentTarget.dataset;
		the.setData({
			'modaldata.op': data.op,
		})
	},
	choose_file_splice:function(e){
		var the =this
		var modaldata =the.data.modaldata
		var data=e.currentTarget.dataset;
		modaldata.file_list.splice(data.index,1);
		the.setData({
			'modaldata.file_list': modaldata.file_list,
		})
	},
	choose_file_keyword: function (e) {
		var the = this
		the.setData({
		  'modaldata.params.title': e.detail.value
		})
	},
	choose_file_image: function (e) {
		var the = this
		var modaldata =the.data.modaldata
		var post =the.data.post
		var att_url=the.data.att_url
		var file=modaldata.file;
		var thumb=modaldata.thumb?modaldata.thumb:'';
		if(e && e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.index>=0){
			var data=e.currentTarget.dataset;
			var file=modaldata.list[data.index].attachment;
			var thumb=modaldata.list[data.index].thumb;
			thumb=thumb?thumb:'';
		}
		if(modaldata.type=='more'){
			console.log(modaldata);
			if(modaldata.tovalue){
						post[modaldata.tovalue][modaldata.index][modaldata.filename]=post[modaldata.tovalue][modaldata.index][modaldata.filename]?post[modaldata.tovalue][modaldata.index][modaldata.filename]:[]
						post[modaldata.tovalue][modaldata.index][modaldata.filename].push(file);
			}else{
						post[modaldata.index][modaldata.filename]=post[modaldata.index][modaldata.filename]?post[modaldata.index][modaldata.filename]:[]
						post[modaldata.index][modaldata.filename].push(file);
			}
			the.setData({
					  pagedata: pagedata,
			})
		}else{
			modaldata.index=modaldata.index?modaldata.index:0
			if(file && modaldata.filename){
				    if(modaldata.tovalue){
						post[modaldata.tovalue][modaldata.index][modaldata.filename]=file
					}else{
						post[modaldata.index][modaldata.filename]=file
					}			
					if(modaldata.thumbname){
						if(modaldata.tovalue){
							post[modaldata.tovalue][modaldata.index][modaldata.thumbname]=thumb
						}else{
							post[modaldata.index][modaldata.thumbname]=thumb
						}
					}
					console.log(post);
					the.setData({
					  'post': post,
					})
			}
		}
		the.setData({
				   showchoosefile:false,
		})		
	},
	choose_file_images:function(){
		var the = this
		var modaldata =the.data.modaldata
		var res=';'+modaldata.file_list.join(';')+';';
		the.setData({
			'file_list': res,
		})
	},
	up_file:function(){
		var the =this
		var modaldata =the.data.modaldata	
		app.util.up_file(modaldata.source_type,function(res,thumb){
			modaldata.file=res
			if(thumb){
				modaldata.thumb=thumb
			}
			the.setData({
				modaldata: modaldata,
			})	
		});
	},
  	// 进云常用函数
	post_diyform:function(e){
		var the = this;
		var index = e.detail.value.index;
		var pageid = e.detail.value.pageid;
		var value = e.detail.value;
		var fileddata =the.data.post.fileddata;
		for (x in value){
			var x = x
			if(x != 'index' && x != 'pageid'){
				var n=x.split("_");
				fileddata[n[1]] = fileddata[n[1]] ? fileddata[n[1]] :{};
				fileddata[n[1]][n[0]] = value[x];
			}
		}
		app.util.post_diyform(fileddata,pageid,index);
	},
	getgroups:function(){
		var that = this;
		var callback=function(groups){
			that.setData({
					groups: groups,
			});
		}
		app.util.get_cmsphotogroups(callback);
	},
	getmoredata:function(){
		var that = this;
		var page = this.data.page;
    var post = {};
        post.page = page;
		var callback=function(list){
			that.setData({
					list: list,
					page: page + 1
			});
		}
		app.util.get_cmsphotos(callback,post);
	},
	ng_input:function(e){
		var that = this;
		var index = e.currentTarget.dataset.index;
		var value = e.detail.value;
		var active = [];
			active[index] = value;
			that.setData({
					ng_active: active,
			})
	},
	ng_active:function(e){
		var that = this;
		var index = e.currentTarget.dataset.index;
		var value = e.currentTarget.dataset.value;
		var active = [];
			active[index] = value;
			that.setData({
					ng_active: active,
			})
	},
  	_openSetting: function(){
		var that = this;
		app.util.openSetting();
		that.setData({
			ShowAuthM:false
		})
	},
	_cancelAuthSetting: function(){
		var that = this;
		app.util.cancelAuthSetting();
		that.setData({
			ShowAuthM:false
		})
	},
	_getUserInfo: function(e){
		var that = this;
		//console.log('_getUserInfo');console.log(e);
		app.util.getUserInfo(e);
		that.setData({
			ShowAuthM:false
		})
	},
  	// 下面是小程序常用API
	wx_chooseImage:function(e){
		var that =this
		var index = e.currentTarget.dataset.index;
		var pindex = e.currentTarget.dataset.pindex;
		wx.chooseImage({
		  count: 1, // 默认9
		  sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
		  sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
		  success: function (res) {
			// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
			var tempFilePaths = res.tempFilePaths
			var chooseimage = {};
				chooseimage[pindex] = {};
			    chooseimage[pindex][index] = tempFilePaths
				that.setData({
					chooseimage: chooseimage,
				})
		  }
		})
	},
	wx_makePhoneCall:function(e){
		var phoneNumber = e.currentTarget.dataset.phone;
		wx.makePhoneCall({
		  phoneNumber: phoneNumber
		})
	},
	// 搜索框文本内容的显示
  inputBind: function(e){
    this.setData({
      keyword:e.detail.value
    })
  },
  //搜索执行按钮
  query:function(e) {
    var page = this;
	var action = e.currentTarget.dataset.action;
	action = action ? action : '//'
    app.util.get(action+'/keyword='+page.data.keyword, function (pagedata) {
      page.setData({
        pagedata: pagedata
      });
	});
  },
  splice:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset
		var p = post.content[data.index][data.childindex]
		post.content[data.index].data=post.content[data.index].data.splice(data.index-1,0,p);		
		post.content[data.index].data=post.content[data.index].data.splice(data.index+1,1)
		the.setData({
			pagedata:pagedata
		})
  },
  spliceItem:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var data = e.currentTarget.dataset
		post.content[data.index].data = post.content[data.index].data.splice(data.index,1)
		the.setData({
			pagedata:pagedata
		})
	},
  info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		app.util.getUserInfo(e);
		this.setData({
			ShowAuthM:false
		})
	},
	closeAuthM:function(){
		var the=this;
		the.setData({
			ShowAuthM:false
		})
	},
	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	onShareAppMessage: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		if (res.from === 'button') {
		}
		var path=the.data.route+'?'+app.access.query;
		if(app.access.op){
			path +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			path +='&fromuser='+pagedata.fromuser;
		}
		return {
		  title: pagedata.sharedata.title,
		  path: path,
		  desc: pagedata.sharedata.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
	onShareTimeline: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		var query=app.access.query
		if(app.access.op){
			query +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			query +='&fromuser='+pagedata.fromuser;
		}
		query +='&isgoshare=1'
		return {
		  title: pagedata.sharedata.title,
		  query: query,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	},
});